package com.baizhi.service.ServiceImpl;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.dao.UserDao;
import com.baizhi.entity.User;
import com.baizhi.entity.UserSexDto;
import com.baizhi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * Created by Administrator on 2019/4/4.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;


    @Override
    @LogAnnotation("查询所有用户")
    public Map selectAllUser(int rows,int page,String username) {
        Map map=new HashMap();
        if(username!=null){
            if("".equals(username)){
                username=null;
            }else{
                username="%"+username+"%";
            }
        }
        int begin=rows*(page-1);
        int size=rows;
        Integer count=userDao.selectAllUserCount(username);
        List<User> users=userDao.selectAllUser( begin,size,username);

        map.put("total",count);
        map.put("rows",users);
        return map;
    }

    @Override
    public Map selectSexCount() {
        Map map=new HashMap();
        List<UserSexDto> userSexDtos=userDao.selectSexCount();
        map.put("sexcount",userSexDtos);
        return map;
    }

    @Override
    public List<Map> selectAddressCount() {
        return userDao.selectAddressCount();
    }

    @Override
    public Map selectDayCount() {
        Map map = new HashMap();
        map.put("one",userDao.selectByDayCount(7,1));
        map.put("two",userDao.selectByDayCount(14,7));
        map.put("three",userDao.selectByDayCount(21,14));
        return map;
    }

    @Override
    public Map appselectUser(Integer userId) {
        Map map = new HashMap();
        User user=userDao.appselectUser(userId);
        map.put("user",user);
        return map;
    }

    @Override
    public Map appUpdateUser(String userPassword,String userNewPassword, Integer userId) {
        Map map = new HashMap();
        try {
            userDao.appUpdateUser(userPassword,userNewPassword,userId);
            map.put("isok",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isok",false);
        }
        return map;
    }


}
